package test.algorithm.FastSlowPointer;



/**
 * 拉丁方阵
 * @author serenity
 *
 */
public class Latin {

	public static void main(String[] args) {
		//创建并初始化有5个节点的链表
		Node header = null;
		Node pointer = header;
		for(int i=1;i<=5;i++){
			Node temp = new Latin.Node(i);
			if(header==null){
				header = temp;
			}else{
				pointer.next = temp;
			}
			pointer = temp;
			pointer.next = header;
		}
		System.out.print("创建5个节点的空链表:");
		printList(header);		
		
		//输出拉丁方阵
		pointer = header;
		System.out.println("拉丁方阵:");
		do{
			// 每行链表都退一格打印
			printList(pointer);
			pointer = pointer.next;
		}while(pointer!=null && pointer!=header);
	}
	
	static class Node {
		private int num;
		private Node next;
		
		public Node(int num){
			this.num = num;
		}
	}
	
	/**
	 * 打印链表
	 * @param node
	 */
	static void printList(Node header){
		Node pointer = header;
		do{
			System.out.print(pointer.num+" ");
			pointer = pointer.next;
		}while(pointer!=null && pointer!=header);
		System.out.println();
	}


}
